﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

using CommonBehaviors.Actions;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Action = Styx.TreeSharp.Action;


namespace Honorbuddy.Quest_Behaviors.SpecificQuests.DebuggingTheTerrace
{
	[CustomBehaviorFileName(@"SpecificQuests\31237-DreadWastes-DebuggingTheTerrace")]
	public class DebuggingTheTerrace : CustomForcedBehavior
	{
		public DebuggingTheTerrace(Dictionary<string, string> args)
			: base(args)
		{
			try
			{
				QuestId = 31237;//GetAttributeAsQuestId("QuestId", true, null) ?? 0;
			}
			catch
			{
				Logging.Write("Problem parsing a QuestId in behavior: Debugging The Terrace");
			}
		}
		public int QuestId { get; set; }
		private bool _isBehaviorDone;
		public int MobIdEyeGas = 63786;
		public int MobIdEyeEmpress = 63783;
		private Composite _root;
		public QuestCompleteRequirement questCompleteRequirement = QuestCompleteRequirement.NotComplete;
		public QuestInLogRequirement questInLogRequirement = QuestInLogRequirement.InLog;
		public override bool IsDone
		{
			get
			{
				return _isBehaviorDone;
			}
		}
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}

		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
			if (!IsDone)
			{
				PlayerQuest Quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
				TreeRoot.GoalText = ((Quest != null) ? ("\"" + Quest.Name + "\"") : "In Progress");
			}
		}

		public List<WoWUnit> Eye
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where(u => u.Entry == MobIdEyeGas && !u.IsDead && u.Distance < 10000).OrderBy(u => u.Distance).ToList();
			}
		}

		public List<WoWUnit> Eye1
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where(u => u.Entry == MobIdEyeEmpress && !u.IsDead && u.Distance < 10000).OrderBy(u => u.Distance).ToList();
			}
		}

		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}

		private bool IsObjectiveComplete(int objectiveId, uint questId)
		{
			if (Me.QuestLog.GetQuestById(questId) == null)
			{
				return false;
			}
			int returnVal = Lua.GetReturnVal<int>("return GetQuestLogIndexByID(" + questId + ")", 0);
			return
				Lua.GetReturnVal<bool>(
					string.Concat(new object[] { "return GetQuestLogLeaderBoard(", objectiveId, ",", returnVal, ")" }), 2);
		}

		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(ret => IsObjectiveComplete(1, (uint)QuestId), new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}

		protected override Composite CreateBehavior()
		{
			return _root ?? (_root = new Decorator(ret => !_isBehaviorDone, new PrioritySelector(
			DoneYet,

			new DecoratorContinue(ret => !IsObjectiveComplete(1, (uint)QuestId),
				new Sequence(  
					new DecoratorContinue(ret => Eye[0].Location.Distance(Me.Location) > 5,
						new Sequence(
							new Action(ret => Navigator.MoveTo(Eye[0].Location)),
							new Action(r => Eye[0].Face()))),
					new DecoratorContinue(ret => Eye[0].Location.Distance(Me.Location) <= 3,
						new Sequence(
							new Action(r => WoWMovement.MoveStop()),
							new Action(r => Eye[0].Face()),
							new Action(r => Eye1[0].Interact()))))),
			new ActionAlwaysSucceed())));
		}
	}
}
